package com.systemutils;

import android.annotation.SuppressLint;
import android.app.Activity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import com.tools.R;
import com.dialogutils.ToastUtil;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 日期：2019年3月23日
 * 公司：北京子杰宝贝科技有限公司
 * 地址：北京市朝阳区朝阳农场
 * 开发者：达选文
 * <p>
 * 主体功能：Activity工具类
 */
public class FragmentUtils {
    private static boolean mBackKeyPressed = false;//记录是否有首次按键

    /**
     * 双击退出
     */
    public static void doubleClickToExit(Activity mActivity) {
        if (!mBackKeyPressed) {
            ToastUtil.showShort(mActivity, "再按一次退出程序");
            mBackKeyPressed = true;
            new Timer().schedule(new TimerTask() {//延时两秒，如果超出则擦错第一次按键记录
                @Override
                public void run() {
                    mBackKeyPressed = false;
                }
            }, 2000);
        } else {
            mActivity.finish();
            System.exit(0);
        }
    }

    /**
     * 退出程序
     */
    public static void clickExit(Activity mActivity) {
        mActivity.finish();
        System.exit(0);
    }

    /**
     * 获取当前显示fragment
     * @return
     * @param mActivity
     */
    public static Fragment getVisibleFragment(FragmentActivity mActivity){
        FragmentManager fragmentManager =mActivity.getSupportFragmentManager();
        return checkFragment(fragmentManager);
    }

    /**
     * 获取当前显示fragment
     * @return
     */
    public static Fragment getVisibleFragment(FragmentManager fragmentManager){
        return checkFragment(fragmentManager);
    }

    private static Fragment checkFragment(FragmentManager frag){
        @SuppressLint("RestrictedApi")
        List<Fragment> fragments = frag.getFragments();
        for (Fragment fragment : fragments) {
            if (fragment != null && fragment.isVisible())
                return fragment;
        }
        return null;
    }

    /**
     * 根据tag移除对应tag的fragment
     * @param mFragmentManager
     * @param tag
     * @return
     */
    public static boolean removeFragmentForTag(FragmentManager mFragmentManager, String tag){
        Fragment mFragment = mFragmentManager.findFragmentByTag(tag);
        if (mFragment != null) {
            mFragmentManager.beginTransaction().remove(mFragment).commit();
            return true;
        }
        return false;
    }

    /**
     * 根据Fragment实列移除对应tag的fragment
     * @param mFragmentManager
     * @return
     */
    public static boolean removeFragment(FragmentManager mFragmentManager,Fragment fragment){
        if (fragment != null) {
            mFragmentManager.beginTransaction().remove(fragment).commit();
            return true;
        }
        return false;
    }

    /**
     * 显示隐藏的fragment
     * @param mFragmentManager
     * @param fragment
     */
    public static void showFragment(FragmentManager mFragmentManager,Fragment fragment){
        if (fragment!=null&&fragment.isHidden()) {
            mFragmentManager.beginTransaction().setCustomAnimations(R.anim.translate_into,R.anim.translate_out).show(fragment).commit();
        }
    }

    /**
     * 显示上一个fragment
     * @param mFragmentManager
     * @param fragment
     * @param tag
     */
    public static void showLastFragment(FragmentManager mFragmentManager,Fragment fragment,String tag){
        removeFragmentForTag(mFragmentManager,tag);
        showFragment(mFragmentManager,fragment);
    }
}
